﻿using System;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Windows.Forms;

namespace CMS.Controls
{
    public class MyTextBox : TextBox
    {
        public MyTextBox()
        {
            //SetStyle(ControlStyles.UserPaint, true);
            //SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            BorderStyle = BorderStyle.FixedSingle;
            //BorderStyle=BorderStyle.None;
        }

        #region 控件绘制相关
        /// <summary>
        /// 文字的布局参数
        /// </summary>
        private readonly StringFormat _StringFormat = new StringFormat() { Alignment = StringAlignment.Near, LineAlignment = StringAlignment.Center };

        /// <summary>
        /// 控件前景色画刷
        /// </summary>
        private Brush _ForeBrush = Brushes.Black;

        /// <summary>
        ///控件背景色画刷
        /// </summary>
        private Brush _BackBrush = Brushes.White;

        /// <summary>
        /// 激活时画笔
        /// </summary>
        private readonly Pen _FocusPen = Pens.SkyBlue;

        protected override void OnForeColorChanged(EventArgs e)
        {
            base.OnForeColorChanged(e);
            _ForeBrush = new SolidBrush(ForeColor);
        }

        protected override void OnBackColorChanged(EventArgs e)
        {
            base.OnBackColorChanged(e);
            _BackBrush = new SolidBrush(BackColor);
        }


        protected override void WndProc(ref Message m)
        {
           
            base.WndProc(ref m);

            if (m.Msg == 0xf || m.Msg == 0x133)
            {
                Graphics g = CreateGraphics();
                //g.SmoothingMode = SmoothingMode.AntiAlias;

                if (Focused )
                {
                    g.DrawRectangle(_FocusPen, 0,0, Width - 1, Height - 1);
                }
                else
                {
                    g.DrawRectangle(Pens.LightGray, 0, 0, Width - 1, Height - 1);
                }

                m.Result = IntPtr.Zero;
                g.Dispose();
            }
        }

        #endregion

    }
}
